package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private TravelMapper travelMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        //排序
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        for (StrategyComment comment : list) {
            String substring = comment.getImgUrls().substring(0, comment.getImgUrls().length() - 1);
            comment.setImgUrls(substring);
        }
        return new PageInfo(list);
    }

    @Override
    public void save(StrategyComment strategyComment) {
        strategyCommentMapper.updateByPrimaryKey(strategyComment);
    }

    @Override
    public List<StrategyComment> selectAll(Long strategyId) {
        List<StrategyComment> strategyComments = strategyCommentMapper.selectAll(strategyId);
        List<StrategyComment> Comments = new ArrayList<>();
        for (StrategyComment strategyComment : strategyComments) {
            String imgUrls = strategyComment.getImgUrls();
            String[] split = imgUrls.split(";");
            strategyComment.setImgUrlss(split);
            Comments.add(strategyComment);
        }
        return Comments;
    }

    @Override
    public void insert(StrategyComment strategyComment) {
        strategyCommentMapper.insert(strategyComment);
    }

    @Override
    public Object updataState(Long id, Long stateId) {//更改状态
        strategyCommentMapper.updataState(id,stateId);
        return null;
    }

    @Override
    public Object getTag() {
        return tagMapper.selectAll();
    }

    @Override
    public int saveStrategyAndComment(StrategyComment strategyComment) {
        strategyComment.setCreateTime(new Date());
        User user = strategyComment.getUser();
        user.setFans(travelMapper.getFansS(user.getId()));
        strategyComment.setUser(user);
        return strategyCommentMapper.saveStrategyAndComment(strategyComment);
    }

    @Override
    public Object getReviewEvaluation(Long id) {//通过评论的id 查询数据
        return strategyCommentMapper.getReviewEvaluation(id);
    }

}
